
package com.Servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Result.BaseRespones;
import com.User.*;
import com.google.gson.Gson;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/json");
		
		String method = request.getParameter("method");
		System.out.println("method:" + method);
		
    	// 获取请求string
		InputStreamReader reader=new InputStreamReader((ServletInputStream)request.getInputStream(),"utf-8");
        BufferedReader br = new BufferedReader(reader);
        StringBuffer sb = new StringBuffer("");
        String temp;
        while((temp = br.readLine()) != null){
            sb.append(temp);
        }
        br.close();
        String requestString=sb.toString();
        //requestString= "{'loginName':'金振明','password':'jinzhenming','userIdentity':3}";
		// 返回结果字符串
		UserOperation useroperation=new UserOperation();
        String reString=null;
		switch (method) {
		case "Login":
			reString=useroperation.userLogin(requestString);
			break;
		case "Register":
			reString=useroperation.userRegist(requestString);
			break;
		case "updatePassword":
			reString=useroperation.userUpdatePasswd(requestString);
			break;
		case "updateLoginName":
			reString=useroperation.userUpdateLoginName(requestString);
			break;
		// TODO: 找回密码
		default:
			break;
		}
		
		PrintWriter out = response.getWriter();
		out.write(reString.replace("\"", "'"));
		out.close();
	}
	
	
	
	


}
